<?php defined('SYSPATH') or die('No direct script access.');


class Controller_Admin_Member extends Controller_Template {

	public function before()
	{
		parent::before();

		$this->checkauth('allow_view_admin');

		$this->model['fill'] = Model::instance('fill');
		$this->set_subtitle('member');
	}

	public function action_list($page = 1, $pagesize = NULL, $order_by = array('timeline' => 'DESC'), $uri = 'admin/member/list')
	{
		$this->checkauth('allow_view_member');

		$page = intval($page);
		is_null($pagesize) && $pagesize = $this->site['pagesize']['admin']['member'];

		$keys = 'username,nickname,gid,timeline,lastlogin';
		$data = Validate::instance()->get_post_data($keys,'member/search','get');
		//if (!empty($data['error'])) //不用查找GET的錯誤，只用得到結果即可
		//	$this->failure_post($data['error']);
		$data = to_array_selector($data, 'data/timelin,lastlogin'); //如果不存在timeline,PHP5.3報錯

		//獲取order_by
		$keys = 'timeline,lastlogin,comment_count,film_timeline';
		$order_by = Validate::instance()->get_order_by($keys, $order_by);

		$member_data = $this->model['member']->search($data['data'], $order_by['original'], $page, $pagesize);
		$member_data = $this->model['fill']->to($member_data, array('gid'=>'gid'));

		$this->template->_member_data = $member_data;
		$this->template->_group_data = $this->model['group']->get_list(); //獲取用戶組列表
		unset($this->template->_group_data[Model_Group::GROUP_VISITOR]);
		//分頁/參數
		$this->template->_param_data = $data['data'];
		$this->template->_url = URL::site($uri, NULL, FALSE).URL::query($data['data'] + array('order_by' => $order_by['original']), FALSE);
		$this->template->_page = $page; 
		$this->template->_order_by = $order_by; 
		$this->template->_page_string = $this->page_multi($member_data['count'], $pagesize, $member_data['page'], $this->template->_url);

		$this->template->set_filename('smarty3:admin/member_list');
	}

	public function action_change_password()
	{

		$this->template->set_filename('smarty3:admin/change_password');
	}

	public function action_change_password_query()
	{
		$this->checkmethod('POST','admin/member/change_password');

		$original_password = $this->request->post('original_password');
		$password = Auth::instance()->hash_password($this->user['username'], $this->request->post('password'));

		if (!Auth::instance()->check_password($original_password))
			$this->failure('setting.failure_original_password');

		$this->model['member']->update($this->user['uid'], compact('password'));

		return $this->success('setting.success_change_password', URL::site('auth/login', NULL, FALSE));
	}

	public function action_suggest_query($keywords = '', $nickname = '', $uid = array(), $gid = array(), $page = 1, $page_size = NULL)
	{
		$page = intval($page);
		is_null($pagesize) && $pagesize = $this->site['pagesize']['admin']['member'];

		$member_data = $this->model['member']->search(array('keywords' => $keywords, 'nickname' => $nickname, 'uid' => $uid, 'gid' => $gid), array('timeline' => 'DESC'), $page, $pagesize);
		$member_data = array_selector($member_data, 'data/*/username,nickname,uid');
		$member_data['data'] = array_values($member_data['data']);
		return $this->success('', TRUE, $member_data);
	}
	
	public function action_check_username_query($username)
	{
		if (empty($username))
			return FALSE;
		$member_data = $this->model['member']->get_byusername($username);
		if (!empty($member_data))
			echo json_encode(FALSE);
		else
			echo json_encode(TRUE);
	}

	public function action_add()
	{
		$this->checkauth('allow_update_member_user');
		$this->checkauth('allow_update_member_admin');

		$keys = 'username,password,sex,usage,nickname,gid,film_timeline,address,mail';
		$this->template->_validate = $this->get_validate($keys,'member/edit');
		$this->template->_group_data = $this->model['group']->get_list(); //獲取用戶組列表
		unset($this->template->_group_data[Model_Group::GROUP_VISITOR]);

		$this->template->set_filename('smarty3:admin/member_add');
	}

	public function action_add_query()
	{
		$this->checkmethod('POST','admin/member/list');

		$this->checkauth('allow_update_member_user');
		$this->checkauth('allow_update_member_admin');

		$gid = intval($this->request->post('gid'));
	
		$keys = 'username,password,sex,usage,nickname,gid,film_timeline,address,mail';
		$data = Validate::instance()->get_post_data($keys,'member/edit');
		if (!empty($data['error'])) 
			return $this->failure_post($data['error']);

		$member_data = $this->model['member']->get_byusername($data['data']['username']);
		if (!empty($member_data))
			return $this->failure('member.failure_username_exists');

		$data['data']['password'] = Auth::instance()->hash_password($data['data']['username'], $data['data']['password']);

		$multi_keys = 'usage';
		$multi_data = array_selector($data['data'], $multi_keys);$data['data'] = array_delete_selector($data['data'], $multi_keys);

		$uid = $this->model['member']->add($this->user['uid'], $data['data']);
		$this->model['member']->add_multi($uid, $multi_keys, $multi_data);
		return $this->success('', URL::site('admin/member/list', NULL, FALSE));
	}

	public function action_edit($uid)
	{
		$this->checkauth('allow_update_member_user');
		$this->checkauth('allow_update_member_admin');

		$uid = intval($uid);
		$data = $this->model['member']->get($uid);
		if (empty($data))
			return $this->failure_noexist();

		$keys = 'password,nickname,sex,usage,gid,film_timeline,address,mail';
		$this->template->_validate = $this->get_validate($keys,'member/edit');

		$this->template->_data = $data;
		$this->template->_group_data = $this->model['group']->get_list(); //獲取用戶組列表
		unset($this->template->_group_data[Model_Group::GROUP_VISITOR]);

		$this->template->set_filename('smarty3:admin/member_edit');
	}


	public function action_edit_query($uid)
	{
		$this->checkmethod('POST','admin/member/list');

		$this->checkauth('allow_update_member_user');
		$this->checkauth('allow_update_member_admin');

		$uid = intval($uid);
		$member_data = $this->model['member']->get($uid);
		if (empty($member_data))
			return $this->failure_noexist();
		
		$keys = 'password,nickname,sex,usage,gid,film_timeline,address,mail';
		$data = Validate::instance()->get_post_data($keys,'member/edit');
		if (!empty($data['error'])) 
			return $this->failure_post($data['error']);

		$password = $this->request->post('password');
		if (!empty($password))
			$data['data']['password'] = Auth::instance()->hash_password($member_data['username'], $password);
		else
			unset($data['data']['password']);

		$multi_keys = 'usage';
		$multi_data = array_selector($data['data'], $multi_keys);$data['data'] = array_delete_selector($data['data'], $multi_keys);

		$this->model['member']->update($uid, $data['data']);
		$this->model['member']->update_multi($uid, $multi_keys, $multi_data);
		return $this->success('', URL::site('admin/member/list', NULL, FALSE));
	}

	public function action_delete_query($uid)
	{
		//$this->checkmethod('POST','admin/member/list');
		$this->checkauth('allow_delete_member_user');
		$this->checkauth('allow_delete_member_admin');

		$uid = intval($uid);
		$member_data = $this->model['member']->get($uid);
		if (empty($member_data))
			return $this->failure_noexist();
		
		$this->model['member']->delete($uid);
		return $this->success('');
	}

}